<div style="padding: 5px;">
	<ul id="codeTree" class="ztree"></ul>
</div>
<script type="text/javascript">
	var codeTable = "${gridId}";
	var setting = {
		async : {
			enable : true,
			url : '${cxt!}/code/treeData',
			autoParam : [ "id=ids" ],//, "name=n", "level=lv"
			type : "post"
		},
		view : {
			fontCss : getFont,
			expandSpeed : "",
			addHoverDom: addHoverDom,
			removeHoverDom : removeHoverDom,
			selectedMulti : false
		},
		edit : {
			enable : true,
			<% if(!authUrl("/code/delCode")){ %>
			showRemoveBtn: false,
			<% } %> 
			showRenameBtn: false,
			removeTitle: "删除"
		},
		data : {
			simpleData : {
				enable : true
			}
		},
		callback : {
			onClick : zTreeOnClick,
			beforeRemove: beforeRemove, //节点被删除之前的事件,并且根据返回值确定是否允许删除操作
			
			beforeExpand : beforeExpand,
			onExpand : onExpand,
			onAsyncSuccess: codeTreeOnAsyncSuccess
		}
	};

	function zTreeOnClick(event, treeId, treeNode) {
		var parm = {
			"_query.parentids" : treeNode.id
		};
		if (treeNode.isParent == false) {
			parm = {
				'_query.ids' : treeNode.id
			}
		}
		$("#" + codeTable).datagrid({
			title : treeNode.name
		});
		$("#" + codeTable).datagrid('load', parm);
	}
	
	//codeTree加载完成后默认展开第一级
	var firstAsyncSuccessFlag = 0;
	function codeTreeOnAsyncSuccess(event, treeId, msg) {  
		if (firstAsyncSuccessFlag == 0) {  
	          try {  
	        	  var codeTree = $.fn.zTree.getZTreeObj("codeTree");
	                 //调用默认展开第一个结点  
	                 var nodes = codeTree.getNodes();  
	                 codeTree.expandNode(nodes[0], true);  
	              
	                 var childNodes = codeTree.transformToArray(nodes[0]);  
	                 codeTree.expandNode(childNodes[1], true);  
	                 firstAsyncSuccessFlag = 1;  
	           } catch (err) {  
	              
	           }  
	              
	     }  
	}

	//字体设置
	function getFont(treeId, node) {
		return node.font ? node.font : {};
	}

	////////////////////下面是处理增删改节点//////////////////

	//节点被删除之前的事件,并且根据返回值确定是否允许删除操作
	function beforeRemove(treeId, treeNode) {
		var zTree = $.fn.zTree.getZTreeObj("codeTree");
		zTree.selectNode(treeNode);

		parent.$.messager.confirm('删除', '确认删除<' + treeNode.name + '>吗？',
				function(r) {
					if (r) {
						var url = "${cxt!}/code/delCode";
						var data = {
							"ids" : treeNode.id
						};
						$.post(url, data, function(result) {
							if(result == '200'){
								zTree.removeNode(treeNode);
								$("#" + codeTable).datagrid('reload');
							}else{
								parent.$.messager.alert('删除', result, 'error');
							}
						});
					}

				});
		return false;

	}
	

	//添加功能按钮
	function addHoverDom(treeId, treeNode) {
		var zTree = $.fn.zTree.getZTreeObj("codeTree");
		var sObj = $("#" + treeNode.tId + "_span");
		if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0 || $("#operatorBtn_" + treeNode.tId).length > 0) {
			return;
		}
		
		
		<% if(authUrl("/code/toUpdate")){ %>
		
		var operatorStr = "<span class='button edit' id='operatorBtn_"
				+ treeNode.tId + "' title='编辑' onfocus='this.blur();'></span>";
		sObj.after(operatorStr);
		var btn = $("#operatorBtn_" + treeNode.tId);
		if (btn)
			btn.bind("click", function() {
				zTree.selectNode(treeNode);
				var url = 'code/toUpdate?ids=' + treeNode.id;
				loadDialog($("#codeTable"), zTree, '编辑', url);
			});
		<% } %>
		
		<% if(authUrl("/code/toAdd")){ %>
		var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
				+ "' title='添加' onfocus='this.blur();'></span>";
		sObj.after(addStr);
		var btn = $("#addBtn_" + treeNode.tId);
		if (btn)
			btn.bind("click", function() {
				zTree.selectNode(treeNode);
				var orderId = (treeNode.children == undefined ? 1
						: (treeNode.children.length + 1));
				var url = 'code/toAdd?code.parentids=' + treeNode.id + '&code.level='
						+ treeNode.level + '&code.orderid=' + orderId;
				loadDialog($("#codeTable"), zTree, '添加', url);
			});
		<% } %>
	}

	function removeHoverDom(treeId, treeNode) {
		//1.处理添加按钮
		$("#addBtn_" + treeNode.tId).unbind().remove();
		//2.处理查看按钮
		$("#operatorBtn_" + treeNode.tId).unbind().remove();
	}

	//////////////////初始化////////////////////

	var curExpandNode = null;
	function beforeExpand(treeId, treeNode) {
		var pNode = curExpandNode ? curExpandNode.getParentNode() : null;
		var treeNodeP = treeNode.parentTId ? treeNode.getParentNode() : null;
		var zTree = $.fn.zTree.getZTreeObj("codeTree");
		for (var i = 0, l = !treeNodeP ? 0 : treeNodeP.children.length; i < l; i++) {
			if (treeNode !== treeNodeP.children[i]) {
				zTree.expandNode(treeNodeP.children[i], false);
			}
		}
		while (pNode) {
			if (pNode === treeNode) {
				break;
			}
			pNode = pNode.getParentNode();
		}
		if (!pNode) {
			singlePath(treeNode);
		}

	}
	function singlePath(newNode) {
		if (newNode === curExpandNode)
			return;

		var zTree = $.fn.zTree.getZTreeObj("codeTree"), rootNodes, tmpRoot, tmpTId, i, j, n;

		if (!curExpandNode) {
			tmpRoot = newNode;
			while (tmpRoot) {
				tmpTId = tmpRoot.tId;
				tmpRoot = tmpRoot.getParentNode();
			}
			rootNodes = zTree.getNodes();
			for (i = 0, j = rootNodes.length; i < j; i++) {
				n = rootNodes[i];
				if (n.tId != tmpTId) {
					zTree.expandNode(n, false);
				}
			}
		} else if (curExpandNode && curExpandNode.open) {
			if (newNode.parentTId === curExpandNode.parentTId) {
				zTree.expandNode(curExpandNode, false);
			} else {
				var newParents = [];
				while (newNode) {
					newNode = newNode.getParentNode();
					if (newNode === curExpandNode) {
						newParents = null;
						break;
					} else if (newNode) {
						newParents.push(newNode);
					}
				}
				if (newParents != null) {
					var oldNode = curExpandNode;
					var oldParents = [];
					while (oldNode) {
						oldNode = oldNode.getParentNode();
						if (oldNode) {
							oldParents.push(oldNode);
						}
					}
					if (newParents.length > 0) {
						zTree.expandNode(oldParents[Math.abs(oldParents.length
								- newParents.length) - 1], false);
					} else {
						zTree.expandNode(oldParents[oldParents.length - 1],
								false);
					}
				}
			}
		}
		curExpandNode = newNode;
	}

	function onExpand(event, treeId, treeNode) {
		curExpandNode = treeNode;
	}

	$(document).ready(function() {
		$.fn.zTree.init($("#codeTree"), setting);
	});
</script>